<?php

namespace app\lnk\service;

use app\appcenter\lib\AppIHookService;
use app\lnk\model\CircleModel;
use app\lnk\model\CircleFansModel;
use app\lnk\model\DynamicModel;
use app\lnk\model\UserModel;
use app\lnk\service\CircleService;
use app\lnk\service\DynamicService;

class OperService
{
    
    public static function _get_circle_list($request)
    {
        $data = CircleModel::where('status', 1)
            ->field('id,avatar,name')
            ->order('weigh', 'desc')
            ->order('create_time', 'desc')
            ->paginate(6)
            ->toArray();
        $data = CircleService::circle_map($data);
        return $data;
    }


    // 用户圈子
    public static function _user_circle($request)
    {
        $uid = $request->uid;
        $user_id = $request->get('user_id', 0);
        if ($user_id > 0) $uid = $user_id;
        $data = CircleFansModel::where('user_id', $uid)
            ->order('id', 'desc')
            ->field('id,circle_id')
            ->select()->toArray();
        if (count($data)) {
            foreach ($data as $k => $v) {
                $circle = CircleModel::where('id', $v['circle_id'])
                    ->field('name,avatar')
                    ->find();
                $circle['is_new'] = DynamicModel::where('circle_id', $v['circle_id'])
                    ->where('create_time', '>', strtotime(date('Y-m-d')))
                    ->count() ? true : false;
                $data[$k]['circle'] = $circle;
            }
        }
        return $data;
    }

    public static function _circle_fans($request)
    {
        $id = $request->get('id');
        $data = CircleFansModel::where('circle_id', $id)
            ->field('id,user_id')
            ->paginate(40)
            ->toArray();
        if (count($data['data'])) {
            foreach ($data['data'] as $k => $v) {
                $data['data'][$k]['user'] = UserModel::where('id', $v['user_id'])
                    ->field('name,avatar')
                    ->find();
            }
        }

        return $data;
    }

}
